﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Common.Serialization;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using ZyGames.Framework.Net;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 10004_用户登录
	/// </summary>
	public class Action10004 : LoginExtendAction
	{
		private GameUser gameUser = null;
		public Action10004(HttpGet httpGet)
			: base((short)ActionType.DZLogin, httpGet)
		{
		}
		protected override bool DoSuccess(int userId, out IUser user)
		{
			user = null;
			var cacheSet = new GameDataCacheSet<GameUser>();
			cacheSet.ReLoad();
			gameUser = cacheSet.FindKey(userId.ToString());
			if (gameUser == null ||
				string.IsNullOrEmpty(gameUser.SessionID) ||
				(Current.User != null && !Current.User.IsOnlining))
			{
				gameUser = cacheSet.FindKey(userId.ToString());
			}

			if (gameUser != null)
			{
				//原因：还在加载中时，返回
				if (gameUser.Property.IsRefreshing)
				{
					ErrorCode = Language.Instance.ErrorCode;
					ErrorInfo = Language.Instance.ServerLoading;
					return false;
				}
			}
			var nowTime = DateTime.Now;
			if (gameUser == null)
			{
				user = new SessionUser() { PassportId = PassportId, UserId = userId };
				this.GuideId = (int)ActionType.DZCreateRole;
				return true;
			}
			user = new SessionUser(gameUser);
			if (gameUser.UserStatus == UserStatus.FengJin)
			{
				ErrorCode = Language.Instance.TimeoutCode;
				ErrorInfo = Language.Instance.AcountIsLocked;
				return false;
			}
			gameUser.SessionID = Sid;
			gameUser.OnlineDate = nowTime;
			gameUser.LoginDate = nowTime;
			gameUser.Property.GameId = this.GameType;
			gameUser.Property.ServerId = this.ServerID;
			gameUser.Property.ChatVesion = 0;

			System.Threading.Tasks.Task.Factory.StartNew(() =>
			{
				//登录日志
				UserLoginLog userLoginLog = new UserLoginLog();
				userLoginLog.UserId = gameUser.GetUserId().ToString();
				userLoginLog.SessionID = Sid;
				userLoginLog.MobileType = (short)this.MobileType;
				userLoginLog.ScreenX = this.ScreenX;
				userLoginLog.ScreenY = this.ScreenY;
				userLoginLog.RetailId = this.RetailID;
				userLoginLog.AddTime = nowTime;
				userLoginLog.State = (int)LoginStatus.Logined;
				userLoginLog.DeviceID = this.DeviceID;
				userLoginLog.Ip = this.GetRealIP();
				userLoginLog.Pid = gameUser.GetPassportId();
				userLoginLog.UserLv = gameUser.UserLv;
				var sender = DataSyncManager.GetDataSender();
				sender.Send(new[] { userLoginLog });
			});
			return true;
		}

		public override void BuildPacket()
		{
			base.BuildPacket();
			try
			{
				//一副牌的数据
				var pockerInfoList = new ShareCacheStruct<PokerInfo>().FindAll();
				var pockerInfo = Convert.ToBase64String(ProtoBufUtils.Serialize(pockerInfoList));
				this.PushIntoStack(pockerInfo);

				//赔率列表
				var outsOddsInfoList = new ShareCacheStruct<OutsOddsInfo>().FindAll();
				var outsOddsInfo = Convert.ToBase64String(ProtoBufUtils.Serialize(outsOddsInfoList));
				this.PushIntoStack(outsOddsInfo);
				//商店
				var shopInfoList = new ShareCacheStruct<ShopInfo>().FindAll();
				var shopInfo = Convert.ToBase64String(ProtoBufUtils.Serialize(shopInfoList));
				this.PushIntoStack(shopInfo);

			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
			if (this.gameUser == null)
				return;
			PushIntoStack(this.gameUser.GetNickName());
			PushIntoStack(this.gameUser.HeadIcon);
			//发送配置信息
		}
	}
}
